package com.second.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.second.client.dao.ClientCouponDao;
import com.second.client.pojo.entity.ClientCoupon;
import com.second.client.pojo.vo.CouponQueryVo;
import com.second.client.pojo.vo.PageVo;
import com.second.client.service.ClientCouponService;
import com.second.client.util.PageData;
import com.second.common.pojo.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import sun.plugin2.gluegen.runtime.CPU;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * (ClientCoupon)表服务实现类
 *
 * @author lvyongqi
 * @since 2021-07-25 09:31:09
 */
@Service("clientCouponService")
public class ClientCouponServiceImpl extends ServiceImpl<ClientCouponDao, ClientCoupon> implements ClientCouponService {

    @Resource
    private ClientCouponDao clientCouponDao;

    /**
     * 查询所有的优惠券信息
     * @param pageVo 分页工具类
     * @param couponQueryVo 模糊查询工具类
     * @return 所有数据
     */
    @Override
    public R getAllCoupon(PageVo pageVo, CouponQueryVo couponQueryVo) {

        // 获取mybatis-plus封装的分页工具类
        Page<ClientCoupon> page = new Page<>(pageVo.getCurrentPage(),pageVo.getPageSize());
        QueryWrapper<ClientCoupon> queryWrapper = new QueryWrapper<>();

        // 判断前端是否传递了模糊查询的规则
        if (couponQueryVo.getCouponType() != null) {
            queryWrapper.eq("coupon_type",couponQueryVo.getCouponType());
        }
        if (StringUtils.isNotEmpty(couponQueryVo.getCouponName())) {
            queryWrapper.like("coupon_name",couponQueryVo.getCouponName());
        }
        if (couponQueryVo.getPlatform() != null) {
            queryWrapper.eq("platform",couponQueryVo.getPlatform());
        }
        if (couponQueryVo.getUseType() != null) {
            queryWrapper.eq("use_type",couponQueryVo.getUseType());
        }
        if (couponQueryVo.getCouponAmount() != null) {
            queryWrapper.eq("coupon_amount",couponQueryVo.getCouponAmount());
        }
        if (StringUtils.isNotEmpty(couponQueryVo.getUseThreshold())) {
            queryWrapper.like("use_threshold",couponQueryVo.getUseThreshold());
        }
        if (StringUtils.isNotEmpty(String.valueOf(couponQueryVo.getCode()))) {
            queryWrapper.eq("code",couponQueryVo.getCode());
        }
        return PageData.conditionalPage(clientCouponDao, page, queryWrapper);

    }

    /**
     * 根据id删除优惠券
     * @param couponId 优惠券Id
     * @return 删除结果
     */
    @Override
    public R deleteCouponById(Integer couponId) {
        boolean remove = this.removeById(couponId);
        if (remove == true) {
            return R.success();
        }
        return R.error();

    }

    /**
     * 修改/添加优惠券信息
     * @param clientCoupon  优惠券信息
     * @return  修改结果
     */
    @Override
    public R updateCoupon(ClientCoupon clientCoupon) {
        boolean b = false;
        if (clientCoupon.getCouponId() != null) {
            // 修改优惠券信息
            b = this.updateById(clientCoupon);
        } else {
            // 设置优惠券码
            clientCoupon.setCode(UUID.randomUUID().toString());
            // 添加优惠券信息
            b = this.save(clientCoupon);
        }
        if (b == true) {
            return R.success();
        }
        return R.error();
    }

    /**
     * 根据id查询优惠券的优惠金额
     * @param couponId  优惠券id
     * @return  优惠金额
     */
    @Override
    public Integer getCouponAmountById(Integer couponId) {
        return this.getById(couponId).getCouponAmount();
    }

}
